<style scoped>
.vendorOrderGoods{
  margin-right: 0.5vw;
  padding: 0 15px;
  background-color: white;
  height: 95vh;
  position: relative;
}
.od-top p:first-child{
  margin: 3px 6px 0 0;
  display: inline-block;
  white-space:nowrap;
}
.od-top p:not(:first-child){
  margin: 3px 6px 0 12px;
  display: inline-block;
  white-space:nowrap;
}
.od-top{
  display: flex;
  padding: 1.5vh 0;
  border-bottom: 2px solid #f4f4f4;
}
.od-btn{
  padding: 1.5vh 0;
  display: flex;
  justify-content: space-between;
}
.od-tab{
  display: flex;
}
.tab-left{
  width: 30%;
  padding-right: 15px;
  border-right: 2px solid #f4f4f4;
}
.tab-right{
  padding-left: 15px;
  width: 70%;
}
.footView span{
    float: left;
    margin-right: 48px;
    line-height: 6vh;
    font-size: 14px;
  }
  .footView span i{
    margin-right: 5px;
  }
</style>

<style>
.vendorOrderGoods .od-top .el-input__inner, .vendorOrderGoods .od-top .el-input{
  height: 30px;
  width: 8vw;
}
.vendorOrderGoods .od-top .el-input{
  width: 8vw;
}
.vendorOrderGoods .el-input__icon{
    line-height: 30px;
  }
.vendorOrderGoods .el-checkbox{
  width: 70px;
  height: 30px;
  padding: 3px 0 0 7px;
}
.vendorOrderGoods .el-table__footer td {
  height: 26px;
}
.vendorOrderGoods .el-table .cell {
    padding:0 0 0 5px;
  }
</style>

<template>
  <div class="vendorOrderGoods">
    <div class="od-top">
      <p>订货日期</p>
      <el-date-picker 
        v-model="inputDate1"
        type="date" 
        placeholder="选择日期" 
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <p>预交日期</p>
      <el-date-picker 
        v-model="inputDate2"
        type="date" 
        placeholder="选择日期" 
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <p>厂商编号</p>
      <el-select v-model="manufacturer">
        <el-option 
          v-for="item in manufacturerList" 
          :key="item.value" 
          :label="item.label" 
          :value="item.value">
          <span style="float:left">{{item.value}}</span>
          <span style="float:right">{{item.label}}</span>
        </el-option>
      </el-select>
    </div>
    <div class="od-btn">
      <div>
        <el-button @click="create" icon="el-icon-plus" type="primary">新建</el-button>
        <el-button @click="Confirm" icon="el-icon-circle-check">确认</el-button>
        <el-button @click="()=>{this.showRevision=true}" icon="el-icon-edit-outline" :disabled="jxChangeOrder">改单</el-button>
        <el-button @click="printReport" icon="el-icon-printer">列印</el-button>
        <el-button @click="dele" icon="el-icon-delete">删除</el-button>
      </div>
      <div style="display:inline-block;">
        <span>合计：{{total}}</span>
        <span style="margin: 0 30px">厂订序号：{{inputSequence}}</span>
        <el-checkbox border v-model="checked">二维</el-checkbox>
      </div>
    </div>
    <div class="od-tab">
      <div class="tab-left">
        <el-table 
          ref="table1" 
          :data="tableData1" 
          style="width: 100%" 
          @row-click="rowClick"
          highlight-current-row 
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'30px'}" 
          :cell-style="{padding:0,height:'26px'}"
          height="75vh" >
          <el-table-column prop="kindsNumber" label="货号" width="160"></el-table-column>
          <el-table-column prop="colorSize" label="色码" width="80"></el-table-column>
          <el-table-column prop="number" label="数量" width="50"></el-table-column>
          <el-table-column prop="commodityName" label="品名" show-overflow-tooltip></el-table-column>
        </el-table>
      </div>
      <div class="tab-right">
        <el-table 
          :data="tableData2" 
          border
          style="width: 100%" 
          :header-cell-style="{background:'#f4f4f4',padding:0,height:'30px'}" 
          :cell-style="{padding:0,height:'26px'}"
          height="75vh"
          ref="table"
          show-summary
          :summary-method="getSummaries">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <template v-for="i in hList">
            <el-table-column 
              :prop="i.prop" 
              :label="i.label" 
              :width="i.width" 
              :key="i.prop"
              show-overflow-tooltip>
            </el-table-column>
          </template>
        </el-table>
      </div>
    </div>
    <foot-view class="footView">
      <div slot="text">
        <span>数据库：{{jxQuarter}}</span>
      </div>
    </foot-view>

    <new-vs 
      v-show="showNew"
      :sendData="sendData"
      :manufacturerList="manufacturerList"
      :flag="showNew"
      @closeNew="()=>{this.showNew=false}"
      @saveNew="saveNew"
      @inpNumChange="()=>{this.inpList = this.getInput()}">
    </new-vs>

    <revision
      v-show="showRevision"
      :flag="showRevision"
      :manufacturerList="manufacturerList"
      :selectInputSequence="inputSequence"
      @closeRevision="()=>{this.showRevision=false}">
    </revision>

    <print-area 
      v-show=false
      isInput='订'
      isVendor="厂商"
      :manufacturerNo="manufacturer"
      :inputSequence="inputSequence"
      :inputDate="inputDate1.slice(0,10)"
      :manufacturerName="printVendorName"
      :principal="principal"
      :phone="phone"
      :address="address"
      :jxQuarter="jxQuarter"
      :printTotal="printTotal"
      :printMoney="printMoney"
      :printStyle="printStyle"
      :printSellingPrice="printSellingPrice"
      :remark="remark"></print-area>
  </div>
</template>

<script>
const resources = require('../../../config/printCSS')

import { getManufacturer } from '../../../network/manufacturer'
import { confirmOrder, orderQuery } from '../../../network/manufacturerOrder'

import footView from '../../../components/FootView'
import newVs from './childComp/newVs'
import revision from './childComp/Revision'
import printArea from '../../../components/PrintVendorStock'

import today from '../../../components/mixins/today'
import enterUp from '../../../components/mixins/enterUp'
import cookieFn from '../../../components/mixins/cookieFn'

export default {
  name: 'vendor_order_goods_vo',
  mixins: [today,enterUp,cookieFn],
  created(){
    this.inpList = this.getInput()
    this.hList = this.hList1
    // 获取厂商信息
    getManufacturer({limit:9999}).then(res=>{
      this.newSend()
      if (res.data.code == 200) {
        let list = res.data.data
        list.forEach(e => {
          this.manufacturerList.push({value:e.no,label:e.name})
        })
      }
      if (this.$route.query.oid) {
        this.inputSequence = this.$route.query.oid
        orderQuery(JSON.stringify({
          orderSalesSequence: this.inputSequence,
          totalWay:0})).then(res => {
          this.showNew = true
          let tmp = res.data.data[this.inputSequence].manufacturerOrderInfoOneVOList[0]
          this.inputDate1 = tmp.orderTime
          this.inputDate2 = tmp.prepayTime
          this.manufacturer = tmp.manufacturerNo
          this.inputSequence = tmp.orderSalesSequence

          this.kindsNumberList = res.data.data[this.inputSequence].manufacturerOrderInfoOneVOList
          this.total = 0
          this.kindsNumberList.forEach(e => {
            e.colorSize = e.colorName + ' ' + e.size
            e.colorNoName = e.colorNo + '.' + e.colorName
            this.total += Number(e.number)
          })
          this.tableData1 = this.kindsNumberList
          this.tableData2 = this.kindsNumberList

          this.conversionList(this.kindsNumberList)
        })
      }
    })
  },
  data(){
    return {
      inpList: [],
      showNew: false,
      showRevision: false,
      readOnly: false,
      sendData: {},

      inputDate1: this.searchDateWithHour(0),
      inputDate2: this.searchDateWithHour(-15),
      manufacturer: '',
      manufacturerList: [],
      inputSequence: '',
      checked: false,

      hList: [],
      hList1:[
        {prop:'kindsNumber',label:'货号',width:'160'},
        {prop:'colorName',label:'颜色',width:'80'},
        {prop:'size',label:'尺码编号',width:'80'},
        {prop:'number',label:'数量',width:'80'},
        {prop:'onePrice',label:'单价',width:'100'},
        {prop:'money',label:'金额',width:'120'},
        {prop:'sellingPrice',label:'售价',width:'100'},
        {prop:'commodityName',label:'品名',width:'160'},
        {prop:'brandedName',label:'品牌',width:'120'},
        {prop:'enteringTime',label:'录入时间',width:'160'}
      ],
		  hList2:[{prop:'styleNumber',label:'款号',width:'140'},
        {prop:'colorNoName',label:'颜色',width:'100'},
        {prop:'c05',label:'05- XS',width:'40'},
        {prop:'c07',label:'07- S',width:'40'},
        {prop:'c09',label:'09- M',width:'40'},
        {prop:'c11',label:'11- L',width:'40'},
        {prop:'c13',label:'13- XL',width:'40'},
        {prop:'c15',label:'15- 2L',width:'40'},
        {prop:'c17',label:'17- 3L',width:'40'},
        {prop:'c19',label:'19- 4L',width:'40'},
        {prop:'c21',label:'21- 5L',width:'40'},
        {prop:'c23',label:'23- 6L',width:'40'},
        {prop:'f',label:'F',width:'40'},
        {prop:'total',label:'小计',width:'60'},
        {prop:'sellingPrice',label:'售价',width:'80'},
        {prop:'sellingPriceMoney',label:'售价金额',width:'100'},
        {prop:'brandedName',label:'品牌',width:''},],

      tableData1: [],
      tableData2: [],

      kindsNumberList: [],
      styleNumberList: [],

      jxChangeOrder: this.getCookie('jxChangeOrder') == '0',
      jxQuarter: this.getCookie('jxQuarter'),

      isConfirm: false,

      // 打印
      styleList: [],
      printTotal: 0,
      printMoney: 0,
      // 款号合计
      printStyle: 0,
      printSellingPrice: 0,
      printVendor: '',
      printVendorName: '',
      principal: '',
      phone: '',
      address: '',
      remark: '',
      total: 0
    }
  },
  watch: {
    inputDate1(a,b){
      this.newNo()
      this.newSend()
    },
    inputDate2(a,b){
      this.newNo()
      this.newSend()
    },
    manufacturer(a,b){
      this.newNo()
      this.newSend()
      getManufacturer({no:a,limit:99999}).then(res => {
        this.printVendorName = res.data.data[0].name
        this.principal = res.data.data[0].principal
        this.phone = res.data.data[0].phone
        this.address = res.data.data[0].address
      })
    },
    checked(a,b){
      if (a) {
        this.hList = this.hList2
        this.tableData2 = this.styleNumberList
      } else {
        this.hList = this.hList1
        this.tableData2 = this.kindsNumberList
      }
    },
    $route(a,b) {
      this.newSend()
      if (b.path.indexOf('delete_document') != -1) {
        this.sequence = ''
        this.tableData1 = []
        this.tableData2 = []
        this.total = 0
        this.kindsNumberList = []
        this.styleNumberList = []
        return
      }
      if (a.path.indexOf('vendor_order_goods_vo') != -1) {
        this.inpList = this.getInput()
      }
    }
  },
  methods: {
    rowClick(e){
    },
    create(){
      this.showNew = true
      this.readOnly = false
      if (!this.$route.query.oid) {
        if (this.isConfirm) this.isConfirm = false
        this.inputSequence = ''
        this.tableData1 = []
        this.tableData2 = []
        this.total = 0
        this.kindsNumberList = []
        this.styleNumberList = []
      }
    },

    // 数据变动
    newSend(){
      this.sendData = {
        inputDate1: this.inputDate1,
        inputDate2: this.inputDate2,
        manufacturer: this.manufacturer,
        inputSequence: this.inputSequence
      }
    },
    saveNew(a){
      // console.log(a)
      this.readOnly = true
      this.isConfirm = false
      if (!a.sequence) return
      this.inputDate1 = a.inputDate1
      this.inputDate2 = a.inputDate2
      this.manufacturer = a.manufacturer
      this.inputSequence = a.sequence
      orderQuery(JSON.stringify({
        orderSalesSequence: a.sequence,
        totalWay: 0
      })).then(res => {
        this.readOnly = false
        if (res.data.code == 200) {
          this.total = 0
          this.kindsNumberList = res.data.data[this.inputSequence].manufacturerOrderInfoOneVOList
          this.kindsNumberList.forEach(e => {
            e.colorSize = e.colorName + ' ' + e.size
            e.colorNoName = e.colorNo + '.' + e.colorName
            this.total += Number(e.number)
          })
          this.checked = false
          this.tableData1 = this.kindsNumberList
          this.tableData2 = this.kindsNumberList
          this.$nextTick(() => {
            this.$refs.table.doLayout()
          })
          this.conversionList(this.kindsNumberList)
        }
      })
    },
    Confirm(){
      // console.log(1)
      if (!this.inputSequence) return
      confirmOrder({sequence: this.inputSequence}).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          this.$message({
            message: '确认成功',
            type: 'success'
          })
          this.isConfirm = true
        } else {
          this.$message.error('确认失败')
        }
      })
    },
    conversionList(list){
      this.styleNumberList = []
      let sn = 0
      let cn = 0

      // 款号颜色
      for (const i of list) {
        if (!this.styleNumberList.length) {
          // 空数组直接添加
          this.styleNumberList.push({
            styleNumber: i.styleNumber,
            colorNo: i.colorNo,
            colorName: i.colorName,
            colorNoName: i.colorNoName,
            c05: 0,
            c07: 0,
            c09: 0,
            c11: 0,
            c13: 0,
            c15: 0,
            c17: 0,
            c19: 0,
            c21: 0,
            c23: 0,
            f: 0,
            total: 0,
            sellingPrice: i.sellingPrice,
            onePrice: i.onePrice,
            commodityName: i.commodityName,
            brandedName: i.brandedName,
            quarter: i.quarter,
            tagPrice: i.sellingPrice
          })
          continue
        }
        for (const j of this.styleNumberList) {
          if (j.styleNumber == i.styleNumber) {
            sn = 1
            // 货号存在，判断颜色有没有
            for (const k of this.styleNumberList) {
              if (i.styleNumber == k.styleNumber && i.colorName == k.colorName) {
                cn = 1
                break
              }
              cn = 0
            }
            if(cn) break
          }
          sn = 0
        }
        
        if (!sn || !cn) {
          // 货号或当前货号的颜色不存在，直接添加
          this.styleNumberList.push({
            styleNumber: i.styleNumber,
            colorNo: i.colorNo,
            colorName: i.colorName,
            colorNoName: i.colorNoName,
            c05: 0,
            c07: 0,
            c09: 0,
            c11: 0,
            c13: 0,
            c15: 0,
            c17: 0,
            c19: 0,
            c21: 0,
            c23: 0,
            f: 0,
            total: 0,
            sellingPrice: i.sellingPrice,
            onePrice: i.onePrice,
            commodityName: i.commodityName,
            brandedName: i.brandedName,
            quarter: i.quarter,
            tagPrice: i.sellingPrice
          })
        }
      }

      // 数量赋值
      list.forEach(e => {
        for (const i of this.styleNumberList) {
          if (e.styleNumber == i.styleNumber && e.colorName == i.colorName ) {
            i[e.relevanceSize] = i[e.relevanceSize]+e.number
            break
          }
        }
      })

      // 计算总数
      this.styleNumberList.forEach(e => {
        e.total = e.c05 + e.c07 + e.c09 + e.c11 + e.c13 + e.c15 + e.c17 + e.c19 + e.c21 + e.c23 + e.f
        e.sellingPriceMoney = e.sellingPrice*e.total
        e.money = e.onePrice*e.total
        if (e.c05 === 0) e.c05 = ''
        if (e.c07 === 0) e.c07 = ''
        if (e.c09 === 0) e.c09 = ''
        if (e.c11 === 0) e.c11 = ''
        if (e.c13 === 0) e.c13 = ''
        if (e.c15 === 0) e.c15 = ''
        if (e.c17 === 0) e.c17 = ''
        if (e.c19 === 0) e.c19 = ''
        if (e.c21 === 0) e.c21 = ''
        if (e.c23 === 0) e.c23 = ''
        if (e.f === 0) e.f = ''
      })
    },

    // 列印
    printReport(){
      this.clearPrintTable()
      this.styleList = []
      this.printTotal = 0
      this.printMoney = 0
      this.printStyle = 0
      this.printSellingPrice = 0
      this.$confirm('是否使用A4纸打印', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        // 用A4纸
        // console.log('用A4纸')
        this.openPrint(true)
      }).catch(action => {
        // 用半张A4纸  
        if (action == 'cancel') {
          // console.log('用半张A4纸')
          this.openPrint(false)
        }
      })
    },
    openPrint(isA4, x=1, id='printTable'){
      // console.log('openPrint')
      let index = --x
      let printTable = document.getElementById(id)
      let length = this.styleNumberList.length
      
      for (index; index < length; index++) {
        const e = this.styleNumberList[index]
        // console.log(index)
        // console.log(index+1)
        this.printTotal += Number(e.total)
        this.printMoney += Number(e.money)
        this.printSellingPrice += Number(e.sellingPriceMoney)

        let row = printTable.insertRow(printTable.FetchRowCount)
        row.insertCell(0).innerHTML = index+1
        row.insertCell(1).innerHTML = e.styleNumber + '-' + e.colorNo
        row.insertCell(2).innerHTML = e.colorName
        row.insertCell(3).innerHTML = e.commodityName
        row.insertCell(4).innerHTML = e.c05
        row.insertCell(5).innerHTML = e.c07
        row.insertCell(6).innerHTML = e.c09
        row.insertCell(7).innerHTML = e.c11
        row.insertCell(8).innerHTML = e.c13
        row.insertCell(9).innerHTML = e.c15
        row.insertCell(10).innerHTML = e.c17
        row.insertCell(11).innerHTML = e.c19
        row.insertCell(12).innerHTML = e.c21
        row.insertCell(13).innerHTML = e.c23
        row.insertCell(14).innerHTML = e.f
        row.insertCell(15).innerHTML = e.total
        row.insertCell(16).innerHTML = e.sellingPrice
        row.insertCell(17).innerHTML = e.sellingPriceMoney

        if (isA4) {
          // console.log(parseInt((index+1)/33))
          // console.log((index+1)%33 == 0)
          if (parseInt((index+1)/33) && (index+1)%33 == 0 && (index+1) != length) {
            // console.log('33倍数')
            let pageIndex = index + 3 - (parseInt(index/33)*33)
            // console.log(pageIndex)
            // console.log(index+1)    // 33
            // console.log(length)     // 36
            let startingPoint = index - 32
            let pageTotal = 0
            let pageMoney = 0
            for (let j = startingPoint; j <= index; j++) {
              pageTotal += Number(this.styleNumberList[j].total)
              pageMoney += Number(this.styleNumberList[j].sellingPriceMoney)
            }

            let rowEnd = printTable.insertRow(printTable.FetchRowCount)
            rowEnd.insertCell(0).innerHTML = '本页小计：'
            printTable.rows[pageIndex].cells[0].colSpan = '2'
            rowEnd.insertCell(1).innerHTML = pageTotal
            printTable.rows[pageIndex].cells[1].colSpan = '2'
            rowEnd.insertCell(2).innerHTML = '汇总金额：'
            printTable.rows[pageIndex].cells[2].colSpan = '4'
            rowEnd.insertCell(3).innerHTML = pageMoney.toFixed(2)
            printTable.rows[pageIndex].cells[3].colSpan = '10'

            let newHead = document.createElement('div')
            newHead.classList.add("addPrintHead")
            newHead.innerHTML = `
              <p class="rightTag" style="top: ${(index+1)/33 * 29.7 + 3}cm;">白联仓库 红联财务 黄联客户</p>
              <span class="printHead">
                ${this.printVendorName}${this.jxQuarter}厂商订货单
              </span>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">厂商：${this.printVendorName}</p>
                <p style="margin: 5px 0;">联系人：${this.principal}</p>
                <p style="margin: 5px 0;">订货序号：${this.inputSequence}</p>
              </div>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">厂商电话：${this.phone}</p>
                <p style="margin: 5px 0;">订货日期：${this.inputDate1.slice(0,10)}</p>
              </div>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">公司地址：${this.address}</p>
              </div>
            `
            newHead.style.pageBreakBefore = 'always'
            this.insertAfter(newHead, printTable)

            let tableId = `printTable${(index+1)/33}`
            // console.log(tableId)
            let tableDom = document.createElement("table")
            tableDom.border = '1'
            tableDom.align = 'center'
            tableDom.id = tableId
            tableDom.innerHTML = `
              <thead>
                <tr>
                  <td style="width: 4%;padding: 0;text-align: center;" rowspan="2">序号</td>
                  <td style="width: 15%;padding: 0;text-align: center;" rowspan="2">型色</td>
                  <td style="width: 8%;padding: 0;text-align: center;" rowspan="2">颜色</td>
                  <td style="width: 15%;padding: 0;text-align: center;" rowspan="2">品名</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">05</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">07</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">09</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">11</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">13</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">15</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">17</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">19</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">21</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">23</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;">F</td>
                  <td rowspan="2" style="text-align: center;width: 5%;">数量</td>
                  <td rowspan="2" style="text-align: center;width: 5%;">售价</td>
                  <td rowspan="2" style="text-align: center;">金额</td>
                </tr>
                <tr>
                  <td style="text-align: center;border-top: 1px dotted;border-left: 1px dotted;">XS</td>
                  <td style="text-align: center;border-top: 1px dotted;">S</td>
                  <td style="text-align: center;border-top: 1px dotted;">M</td>
                  <td style="text-align: center;border-top: 1px dotted;">L</td>
                  <td style="text-align: center;border-top: 1px dotted;">XL</td>
                  <td style="text-align: center;border-top: 1px dotted;">2L</td>
                  <td style="text-align: center;border-top: 1px dotted;">3L</td>
                  <td style="text-align: center;border-top: 1px dotted;">4L</td>
                  <td style="text-align: center;border-top: 1px dotted;">5L</td>
                  <td style="text-align: center;border-top: 1px dotted;">6L</td>
                  <td style="text-align: center;border-top: 1px dotted;"></td>
                </tr>
              </thead>
            `
            // console.log(tableDom)
            // console.log(index)
            this.printStyleCount(e.styleNumber)
            this.insertAfter(tableDom, newHead)
            this.openPrint(true,index+2,tableId)
            return
          }
          if ((index+1) == length) {
            let pageIndex = index + 3 - (parseInt(index/33)*33)
            // console.log('最后一行')
            let startingPoint = index < 33 ? 0 : parseInt(index/33)*33
            let pageTotal = 0
            let pageMoney = 0
            for (let j = startingPoint; j <= index; j++) {
              pageTotal += Number(this.styleNumberList[j].total)
              pageMoney += Number(this.styleNumberList[j].sellingPriceMoney)
            }
            let rowEnd = printTable.insertRow(printTable.FetchRowCount)
            rowEnd.insertCell(0).innerHTML = '本页小计：'
            printTable.rows[pageIndex].cells[0].colSpan = '2'
            rowEnd.insertCell(1).innerHTML = pageTotal
            printTable.rows[pageIndex].cells[1].colSpan = '2'
            rowEnd.insertCell(2).innerHTML = '汇总金额：'
            printTable.rows[pageIndex].cells[2].colSpan = '4'
            rowEnd.insertCell(3).innerHTML = pageMoney.toFixed(2)
            printTable.rows[pageIndex].cells[3].colSpan = '10'
          }
        } else {
          if (parseInt((index+1)/12) && (index+1)%12 == 0 && (index+1) != length) {
            // console.log('12倍数')
            let pageIndex = index + 3 - (parseInt(index/12)*12)
            // console.log(pageIndex)
            // console.log(index+1)    // 12
            // console.log(length)     // 36
            let startingPoint = index - 11
            let pageTotal = 0
            let pageMoney = 0
            for (let j = startingPoint; j <= index; j++) {
              pageTotal += Number(this.styleNumberList[j].total)
              pageMoney += Number(this.styleNumberList[j].sellingPriceMoney)
            }

            let rowEnd = printTable.insertRow(printTable.FetchRowCount)
            rowEnd.insertCell(0).innerHTML = '本页小计：'
            printTable.rows[pageIndex].cells[0].colSpan = '2'
            rowEnd.insertCell(1).innerHTML = pageTotal
            printTable.rows[pageIndex].cells[1].colSpan = '2'
            rowEnd.insertCell(2).innerHTML = '汇总金额：'
            printTable.rows[pageIndex].cells[2].colSpan = '4'
            rowEnd.insertCell(3).innerHTML = pageMoney.toFixed(2)
            printTable.rows[pageIndex].cells[3].colSpan = '10'

            let newHead = document.createElement('div')
            newHead.classList.add("addPrintHead")
            newHead.innerHTML = `
              <p class="rightTag" style="top: ${(index+1)/12 * 14.85 + 3}cm;">白联仓库 红联财务 黄联客户</p>
              <span class="printHead">
                ${this.printVendorName}${this.jxQuarter}厂商订货单
              </span>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">厂商：${this.printVendorName}</p>
                <p style="margin: 5px 0;">联系人：${this.principal}</p>
                <p style="margin: 5px 0;">订货序号：${this.inputSequence}</p>
              </div>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">厂商电话：${this.phone}</p>
                <p style="margin: 5px 0;">订货日期：${this.inputDate1.slice(0,10)}</p>
              </div>
              <div class="info" style="height: 25px;">
                <p style="margin: 5px 0;">公司地址：${this.address}</p>
              </div>
            `
            newHead.style.pageBreakBefore = 'always'
            this.insertAfter(newHead, printTable)

            let tableId = `printTable${(index+1)/12}`
            // console.log(tableId)
            let tableDom = document.createElement("table")
            tableDom.border = '1'
            tableDom.align = 'center'
            tableDom.id = tableId
            tableDom.innerHTML = `
              <thead>
                <tr>
                  <td style="width: 4%;padding: 0;text-align: center;" rowspan="2">序号</td>
                  <td style="width: 15%;padding: 0;text-align: center;" rowspan="2">型色</td>
                  <td style="width: 8%;padding: 0;text-align: center;" rowspan="2">颜色</td>
                  <td style="width: 15%;padding: 0;text-align: center;" rowspan="2">品名</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">05</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">07</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">09</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">11</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">13</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">15</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">17</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">19</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">21</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;" nowrap="nowrap">23</td>
                  <td style="width: 3.8%;padding: 0;text-align: center;">F</td>
                  <td rowspan="2" style="text-align: center;width: 5%;">数量</td>
                  <td rowspan="2" style="text-align: center;width: 5%;">售价</td>
                  <td rowspan="2" style="text-align: center;">金额</td>
                </tr>
                <tr>
                  <td style="text-align: center;border-top: 1px dotted;border-left: 1px dotted;">XS</td>
                  <td style="text-align: center;border-top: 1px dotted;">S</td>
                  <td style="text-align: center;border-top: 1px dotted;">M</td>
                  <td style="text-align: center;border-top: 1px dotted;">L</td>
                  <td style="text-align: center;border-top: 1px dotted;">XL</td>
                  <td style="text-align: center;border-top: 1px dotted;">2L</td>
                  <td style="text-align: center;border-top: 1px dotted;">3L</td>
                  <td style="text-align: center;border-top: 1px dotted;">4L</td>
                  <td style="text-align: center;border-top: 1px dotted;">5L</td>
                  <td style="text-align: center;border-top: 1px dotted;">6L</td>
                  <td style="text-align: center;border-top: 1px dotted;"></td>
                </tr>
              </thead>
            `
            // console.log(tableDom)
            // console.log(index)
            this.printStyleCount(e.styleNumber)
            this.insertAfter(tableDom, newHead)
            this.openPrint(false,index+2,tableId)
            return
          }
          if ((index+1) == length) {
            let pageIndex = index + 3 - (parseInt(index/12)*12)
            // console.log('最后一行')
            let startingPoint = index < 12 ? 0 : parseInt(index/12)*12
            let pageTotal = 0
            let pageMoney = 0
            for (let j = startingPoint; j <= index; j++) {
              pageTotal += Number(this.styleNumberList[j].total)
              pageMoney += Number(this.styleNumberList[j].sellingPriceMoney)
            }
            let rowEnd = printTable.insertRow(printTable.FetchRowCount)
            rowEnd.insertCell(0).innerHTML = '本页小计：'
            printTable.rows[pageIndex].cells[0].colSpan = '2'
            rowEnd.insertCell(1).innerHTML = pageTotal
            printTable.rows[pageIndex].cells[1].colSpan = '2'
            rowEnd.insertCell(2).innerHTML = '汇总金额：'
            printTable.rows[pageIndex].cells[2].colSpan = '4'
            rowEnd.insertCell(3).innerHTML = pageMoney.toFixed(2)
            printTable.rows[pageIndex].cells[3].colSpan = '10'
          }
        }

        this.printStyleCount(e.styleNumber)
      }

      this.printStyle = this.styleList.length
      // console.log(this.styleList)
      let int = setInterval(() => {
        if (this.printStyle) {
          clearInterval(int)
          let printHtml = document.getElementById('p').innerHTML
          let wind = window.open("",'newwindow', 'height=500, width=1000, top=100, left=100, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')
          wind.document.body.innerHTML = printHtml
          let style = document.createElement('style')
          style.type = 'text/css'
          style.innerHTML = resources.printCSS
          wind.document.head.appendChild(style)
          wind.print()
          return false
        }
      }, 500)
    },
    clearPrintTable(){
      let printTable = document.getElementById("printTable")
      let trs = printTable.getElementsByTagName("tr")
      let len = trs.length - 1
      for(let i = len; i > 1; i--) {
        printTable.deleteRow(i)
      }

      let printArea = document.getElementById("p")
      let tables = printArea.getElementsByTagName('table')
      len = tables.length
      for (let i = 1; i < len; i++) {
        tables[1].remove()
      }
      let addPrintHeads = document.getElementsByClassName('addPrintHead')
      len = addPrintHeads.length
      for (let i = 0; i < len; i++) {
        addPrintHeads[0].remove()
      }
    },
    printStyleCount(s){
      if (!this.styleList.length) {
        this.styleList.push(s)
      } else {
        let exist = 0
        for (const i of this.styleList) {
          // console.log(i)
          if (i == s) {
            exist = 1
            break
          }
        }
        if (!exist) {
          this.styleList.push(s)
        }
      }
    },
    // insertAfter(p,header1) // 因为js没有直接追加到指定元素后面的方法 所以要自己创建一个方法
    insertAfter( newElement, targetElement ){ // newElement是要追加的元素 targetElement 是指定元素的位置
      let parent = targetElement.parentNode // 找到指定元素的父节点
      if( parent.lastChild == targetElement ){ // 判断指定元素的是否是节点中的最后一个位置 如果是的话就直接使用appendChild方法
        parent.appendChild( newElement, targetElement )
      }else{
        parent.insertBefore( newElement, targetElement.nextSibling )
      }
    },
    dele(){
      if (!this.inputSequence) return
      this.$router.push({path: '/delete_document', query: {type: 8,sequence: this.inputSequence}})
      this.$store.commit('addNav',{label:'厂商订货删单',name:'delete_document'})
    },

    getSummaries(param){
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const prop = column.property
        const values = data.map(item => Number(item[prop]))
        if (!values.every(value => isNaN(value))) {
          if (prop == 'c05' || prop == 'c07' || prop == 'c09' || prop == 'c11' || prop == 'c13' || prop == 'c15' || prop == 'c17' || prop == 'c19' || prop == 'c21' || prop == 'c23' || prop == 'f' || prop == 'total' || prop == 'number') {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            if (!sums[index]) {
              sums[index] = ''
            }
            let decimal = sums[index].toString().split('.')[1]
            if (decimal && decimal.length > 2) {
              sums[index] = sums[index].toFixed(2)
            }
          }
        }
      })
      return sums
    },

    newNo(){
      if (this.$route.query.oid || this.readOnly) return
      this.sequence = ''
      this.tableData1 = []
      this.tableData2 = []
      this.total = 0
      this.kindsNumberList = []
      this.styleNumberList = []
    }
  },
  components:{
    footView,
    newVs,
    revision,
    printArea
  },
}
</script>